/**
 * Finds all the fonts used in the library and outputs a list of them and 
 * their container clips in the output window. It Then provides two prompts
 * for finding/replacing font names. This is useful for resolving inconsistant
 * font names, such as "Arial" vs. "arial".
 *
 * The Find/Replace dialogs loop, allowing you to make multiple find/replaces
 * and see the results re-output to the output panel. To exit the find/replace
 * loops, just choose Cancel.
 */

fl.outputPanel.clear();
findFonts();

function findFonts()
{
	var list = fetchFonts();
	var replace = prompt("Enter the Font Name you wish to REPLACE", "");
    while(replace != null && replace != "")
	{
		var replaceWith = prompt("Enter the name of the new font");
		
		fl.outputPanel.clear();
		fl.trace("Re-scanning for fonts...");
		
		list = fetchFonts(replace, replaceWith);
		replace = prompt("Enter the Font Name you wish to REPLACE", "");
	}
	
	
}

/**
 * Scans the supplied flash library for linked classes and adds them to the supplied empty array.
 * @param 	library		A flash library to scan.
 * @param	arr			An empty array to populate.
 */
function fetchFonts(toReplace, newVal)
{
	var doc = fl.getDocumentDOM();	
	var masterList = [];
	var timeline = doc.getTimeline();
	
	var replaceCount = scanTimeline( timeline, masterList, toReplace, newVal, timeline.name );
	
	// populate the list with all classes referenced in the library.
	replaceCount += scanLibrary( doc.library, masterList, toReplace, newVal );
    
	masterList.sort( function (a, b){return (a.face > b.face) - (a.face < b.face);} );
	
	if(replaceCount != 0)
	{
		fl.trace("Replaced " + replaceCount + " Textfields with " + newVal);
	}
	
	for(var i = 0; i < masterList.length; i++)
    {
		fl.trace(masterList[i].face + " -> " + masterList[i].layer + " [ " + masterList[i].frame + " ] " + masterList[i].name);
	}
	
	return masterList;
}

/**
 * Scans the supplied flash library for linked classes and adds them to the supplied empty array.
 * @param 	library		A flash library to scan.
 * @param	arr			An empty array to populate.
 */
function scanLibrary( library, arr, toReplace, newValue )
{
    var items = library.items;
    var item;
	var replaceCount = 0;
  
    for( var i = 0; i < items.length; i++ )
    {
        item = items[i];
		if (item.itemType == 'movie clip') 
		{
			var timeline = item.timeline;
			library.selectItem(item);
			library.editItem();
			scanTimeline( timeline, arr, toReplace, newValue, item.name );
			
		}
    }
	return replaceCount;
}

function scanTimeline( timeline, arr, toReplace, newValue, name )
{
	var h = timeline.layerCount;
	while(h--)
	{
		var k = timeline.layers[h].frameCount;
		
		while(k--)
		{
			if( timeline.layers[h].frames.length > 0 )
			{

				var elems = timeline.layers[h].frames[k].elements;
				var p = elems.length;
				
				while(p--)
				{
					if(elems[p].elementType == "text")
					{
						if(toReplace != null && newValue != null && elems[p].getTextAttr("face") == toReplace)
						{
							elems[p].setTextAttr("face", newValue);
							replaceCount++;
						}

						arr.push({face:elems[p].getTextAttr("face"), name:name, frame:k, layer:timeline.layers[h].name});
					}
				}
			}
		}
	}
}

